package com.banshuibao.bsb_android.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.OfficeListAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.OfficeModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;

/**
 * Created by zyh on 2017/12/20.
 */

public class OfficeListFragment extends BaseFragment implements PullToRefreshBase.OnRefreshListener2<ListView> {

    private PullToRefreshListView pullToRefreshListView;

    private ListView listView;

    private OfficeListAdapter adapter;

    private int sinceId = 1;

    public OfficeListFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_office_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        pullToRefreshListView = view.findViewById(R.id.pull);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(this);
        listView = pullToRefreshListView.getRefreshableView();
        listView.setDivider(null);
        adapter = new OfficeListAdapter(getActivity());
        listView.setAdapter(adapter);
        loadNewData();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        loadNewData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        loadOfficeData(sinceId, sinceId + 10);
    }

    private void loadNewData() {
        sinceId = 1;
        int end = adapter != null && adapter.getCount() > 10 ? adapter.getCount() : 10;
        loadOfficeData(sinceId, end);
    }

    /**
     * 获取事务所信息 0动态 1商品
     **/
    void loadOfficeData(final int s, final int e) {
        OfficeModel.Body body = new OfficeModel.Body();
        body.setSinceId(s);
        body.setMaxId(e);
        HttpUtil.doPost(Constants.Url.LIST_MOVING, body, new HttpResponse(getActivity(), OfficeModel.class) {
            @Override
            public void onResult(Object obj) {
                OfficeModel officeModel = (OfficeModel) obj;
                sinceId += officeModel.getRows();
                if (adapter != null) {
                    if (s == 1)
                        adapter.setData(officeModel.getData());
                    else
                        adapter.addData(officeModel.getData());
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullComplete();
            }
        });
    }

    private void pullComplete() {
        pullToRefreshListView.onRefreshComplete();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.clear();
            adapter = null;
        }
    }
}
